package com.uenpay.im.ui.old.activity.funds;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.utils.Utils;
import com.qyx.android.message.type.PacketType;
import com.qyx.android.protocol.SessionModel;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.chat.ChatActivity;
import com.uenpay.im.ui.old.activity.group.AddGroupMemberActivity;
import com.uenpay.im.ui.old.activity.group.GroupMangerHandler;
import com.uenpay.util.app.base.BaseActivity;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 * @E-mail:
 * @date 创建时间：2016-5-9 下午4:39:25
 * @version 1.0
 * @function 修改入群金额
 */
public class UpdateJoinGroupAmountActivity extends BaseActivity {

	private Button comfirm_btn;
	private EditText amount_et;
	private View loading;
	private String cust_ids, chat_name,chat_id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		isFullScreen=true;
		super.onCreate(savedInstanceState);
	}

	@Override
	public View bindView() {
		return null;
	}

	@Override
	public int bindLayout() {
		return R.layout.activity_update_join_group_amount_layout;
	}

	@Override
	public void initView(View view) {
		if (getIntent() != null) {
			if (getIntent().hasExtra("chat_name")) {
				chat_name = getIntent().getStringExtra("chat_name");
			}
			if (getIntent().hasExtra("cust_ids")) {
				cust_ids = getIntent().getStringExtra("cust_ids");
			}
			if (getIntent().hasExtra("chat_id")) {
				chat_id = getIntent().getStringExtra("chat_id");
			}
		}
		initView();
		initListener();
	}

	protected void initView() {
		((TextView) findViewById(R.id.title_textview))
				.setText(R.string.update_join_group_amount);
		comfirm_btn = (Button) findViewById(R.id.comfirm_btn);
		amount_et = (EditText) findViewById(R.id.amount_et);
		loading = findViewById(R.id.loading);

		if (!TextUtils.isEmpty(cust_ids)) {
			((TextView) findViewById(R.id.title_textview))
					.setText(R.string.set_join_group_amount);
			comfirm_btn.setText(R.string.start_group_chat);
		}
	}

	protected void initListener() {
		findViewById(R.id.back_layout).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View arg0) {
						Utils.hideSoftKeyboard(amount_et);
						finish();
					}
				});
		amount_et.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				checkBtnStatus();
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
			}

			@Override
			public void afterTextChanged(Editable arg0) {
			}
		});

		comfirm_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				String amount = amount_et.getText().toString();
				if (!TextUtils.isEmpty(amount) && !TextUtils.isEmpty(cust_ids)) {
					createFundsGroup(amount);
				}else{
					setGroupFundsAmount(amount);
				}
			}
		});
	}

	private void checkBtnStatus() {
		String amount = amount_et.getText().toString();
		if (!TextUtils.isEmpty(amount)) {
			comfirm_btn.setBackgroundResource(R.drawable.button_bg_normal);
			comfirm_btn.setEnabled(true);
		} else {
			comfirm_btn.setEnabled(false);
			comfirm_btn.setBackgroundResource(R.drawable.button_bg_gray);
		}
	}

	private void createFundsGroup(String amount) {
		loading.setVisibility(View.VISIBLE);
		GroupMangerHandler mGroupMangerHandler = new GroupMangerHandler();
		chat_name = getResources().getString(R.string.funds_group);
		mGroupMangerHandler.createGroup(chat_name, cust_ids, amount,
				new GroupMangerHandler.IGroupManagerResultListener() {

					@Override
					public void onGroupManagerResult(int status, String msg,
							String chatId) {
						loading.setVisibility(View.GONE);
						if (status == HttpInvokeResultCode.RESULT_OK) {
							if (!TextUtils.isEmpty(chatId)) {
								startTalk(chatId);
							}
						} else if (!TextUtils.isEmpty(msg)) {
							IMApplication.showToast(msg);
						}
					}
				});
	}

	private void startTalk(String chat_id) {
		Intent intent = new Intent(UpdateJoinGroupAmountActivity.this,
				ChatActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		Bundle bu = new Bundle();
		SessionModel sessionModel = new SessionModel();
		sessionModel.setSessionId(Long.valueOf(chat_id));
		sessionModel.setOperationType(3);
		sessionModel.setSessionType(PacketType.GROUP);
		bu.putParcelable("session", sessionModel);
		intent.putExtras(bu);
		startActivity(intent);
		if(AddGroupMemberActivity.mAddGroupMemberActivity != null){
			AddGroupMemberActivity.mAddGroupMemberActivity.finish();
		}
		IMApplication.IS_REFRESH_TOP_MSG = true;
		finish();
	}

	private void setGroupFundsAmount(String amount){
		loading.setVisibility(View.VISIBLE);
		FundsHandle.setGroupFundsAmount(chat_id, amount, new FundsHandle.IFundsListener() {

			@Override
			public void onFundsResult(int status, String msg) {
				loading.setVisibility(View.GONE);
				if(status == HttpInvokeResultCode.RESULT_OK){
					setResult(RESULT_OK);
					finish();
				}else if(!TextUtils.isEmpty(msg)){
					IMApplication.showToast(msg);
				}
			}
		});
	}

	@Override
	public void onBackPressed() {
		finish();
		super.onBackPressed();
	}
}
